package org.algorithm.Recursion;

import java.util.Arrays;

//冒泡排序
public class BubbleSortUseRecursion
{

    public void sort(int [] array)
    {
        bubblePlus(array,array.length-1);
    }

    //right表示没有排序的右边界
    private void bubble(int [] array , int right)
    {
        if (right==0)
        {
            return;
        }

        for (int i = 0;i<right;i++)
        {
            if (array[i]>array[i+1])
            {
                int temp = array[i];
                array[i]=array[i+1];
                array[i+1]=temp;
            }
        }

        bubble(array,right-1);
    }

    //border表示未排序和排序的边界，减少递归的次数
    private void bubblePlus(int [] array , int right)
    {
        if (right==0)
        {
            return;
        }

        int border=0;

        for (int i = 0;i<right;i++)
        {
            if (array[i]>array[i+1])
            {
                int temp = array[i];
                array[i]=array[i+1];
                array[i+1]=temp;
                border=i;
            }
        }

        bubble(array,border);
    }


    public static void main(String[] args) {
        BubbleSortUseRecursion recursion = new BubbleSortUseRecursion();

        int []array={1,2,3,6,4,7};
        System.out.println( Arrays.toString(array));
        recursion.sort(array);
        System.out.println("----------------------");
        System.out.println( Arrays.toString(array));

    }
}
